//
//  ShopWebController.m
//  懒猫
//
//  Created by jike on 15/9/18.
//  Copyright (c) 2015年 ZM. All rights reserved.
//

#import "ShopWebController.h"
#import "UINavigationController+SGProgress.h"
#import "JsWebviewNativeManager.h"
#import "AFNetworkHandler.h"
@interface ShopWebController ()<UIWebViewDelegate>

@property(nonatomic, getter = isCanceled) BOOL canceled;

@property (nonatomic, retain) UIView *lowerView;
@property (nonatomic, retain) UIButton *subtraction;
@property (nonatomic, retain) UIButton *addition;
@property (nonatomic, retain) UILabel *count;
@property (nonatomic, retain) NSString *countNum;// 商品数量
@property (nonatomic, retain) UILabel *carLable;



@end

@implementation ShopWebController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    
    [self initNav:self.titleName doBack:YES];


    self.countNum = @"0";
    
    // 创建视图
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight)];
    webView.delegate = self;
    // 设置webView上的scrollView边界预留   // ScreenHeight * 0.09
    [webView.scrollView setContentInset:UIEdgeInsetsMake(0, 0, 64, 0)];
    // 边缘弹动效果
    [webView.scrollView setBounces:NO];
    // 加载请求
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webUrl]]];
    // 添加父视图
    [self.view addSubview:webView];
    
    
    
    self.lowerView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 128, ScreenWidth, 64)];
    self.lowerView.backgroundColor = [UIColor whiteColor] ;
    [webView addSubview:self.lowerView];
    
    UIView *linView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
    linView.backgroundColor = BackColor;
    [self.lowerView addSubview:linView];
    
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 100, 20)];
    lable.text = @"添加商品:";
    [self.lowerView addSubview:lable];

    
    // 减号button
    self.subtraction = [[UIButton alloc] init];
    self.subtraction.frame = CGRectMake(100, 15, 25, 25);
    //    [self.subtraction setTitle:@"-" forState:UIControlStateNormal];
    [self.subtraction setImage:[UIImage imageNamed:@"首页_r17_c18"] forState:UIControlStateNormal];
    [self.subtraction addTarget:self action:@selector(subtractionAction) forControlEvents:UIControlEventTouchUpInside];
    [self.subtraction setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.subtraction.layer.borderWidth = 1;
    self.subtraction.layer.borderColor = [UIColor grayColor].CGColor;
    self.subtraction.backgroundColor = [UIColor whiteColor];
    self.subtraction.layer.cornerRadius = 25 / 2;
    self.subtraction.clipsToBounds = YES;
    [self.lowerView addSubview:self.subtraction];
    
    
    // 加号button
    self.addition = [[UIButton alloc] init];
    self.addition.frame = CGRectMake(160, 15, 25, 25);
    //    [self.addition setTitle:@"+" forState:UIControlStateNormal];
    [self.addition setImage:[UIImage imageNamed:@"首页_r21_c25"] forState:UIControlStateNormal];
    [self.addition setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.addition addTarget:self action:@selector(additionAction) forControlEvents:UIControlEventTouchUpInside];
    self.addition.layer.borderWidth = 1;
    self.addition.layer.borderColor = [UIColor grayColor].CGColor;
    self.addition.backgroundColor = [UIColor whiteColor];
    self.addition.layer.cornerRadius = 25 / 2;
    self.addition.clipsToBounds = YES;
    [self.lowerView addSubview:self.addition];
    
    // 数量
    self.count = [[UILabel alloc]init];
    self.count.frame = CGRectMake(120, 20, 50, 20);
//        self.count.backgroundColor = [UIColor blackColor];
    self.count.textAlignment = NSTextAlignmentCenter;
    self.count.text = self.countNum;
    [self.lowerView addSubview:self.count];
    
    // 购物车
    UIButton *shopCar = [[UIButton alloc] initWithFrame:CGRectMake(300, 5, 50, 50)];
    [shopCar setImage:[UIImage imageNamed:@"搜索产品购物车_r2_c2"] forState:UIControlStateNormal];
    shopCar.layer.cornerRadius = 25;

    shopCar.adjustsImageWhenHighlighted = NO;
    [shopCar addTarget:self action:@selector(shopAction) forControlEvents:UIControlEventTouchUpInside];
    shopCar.backgroundColor = [UIColor orangeColor];
    shopCar.clipsToBounds = YES;
    [self.lowerView addSubview:shopCar];
    
    // 圈圈
    self.carLable = [[UILabel alloc] initWithFrame:CGRectMake(330, 5, 20, 20)];
    self.carLable.layer.cornerRadius = 10;
    self.carLable.clipsToBounds = YES;
    self.carLable.text = self.countNum;
    self.carLable.backgroundColor = [UIColor redColor];
    self.carLable.textColor = [UIColor whiteColor];
    self.carLable.textAlignment = NSTextAlignmentCenter;
    [self.lowerView addSubview:self.carLable];
    
    
}

#warning 分享按钮
-(void)shareAction
{
   
}

#warning 跳转到购物车页面
-(void)shopAction
{
    [[AppData share].baseTabVC changeBottomBtnWithIndex:2];
//    [JsWebviewNativeManager  theRequestUrl:@"opencart" viewController:self success:^(JsWithWB type, NSString *count) {
//        
//        
//    }];
    
}


#warning 加号方法
-(void)additionAction
{
    // 判断是否登录
    if (![AppData share].doLogin) {
        [[MessShowView share] showWithMess:@"请先登录"];
        [self goLoginView];
        return;
    }else{
    
    NSString *url = [NSString stringWithFormat:@"http://mtest.ivpin.com//WPT-OpenAPI?control=LCatShop&action=upateShopCart&username=%@&shopuser=%@&productid=%@&type=1&sign=",[AppData share].username,[ShopCommonModel getShopMess].shopuser,self.productID];
    
    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
        NSDictionary *tempDic = [NSDictionary dictionary];
        tempDic = result;
 
        if ([[tempDic objectForKey:@"r"] isEqualToString:@"T"]) {
            NSInteger tempNum = [self.countNum integerValue];
                tempNum ++;
                self.countNum = [NSString stringWithFormat:@"%ld",tempNum];
                self.count.text = self.countNum;
                self.carLable.text = self.countNum;
                NSString *strNum = [NSString stringWithFormat:@"%ld",tempNum];
                
                // 小红点
                [[AppData share].baseTabVC theShoppingCartCount:strNum];
            }else{
                
//            UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"补货中" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
//            [aler show];
            
        }
    }];
        
    }// 登录

}

#warning 减号按钮
-(void)subtractionAction
{
    // 判断是否登录
    if (![AppData share].doLogin) {
        [[MessShowView share] showWithMess:@"请先登录"];
        [self goLoginView];
        return;
    }else{

    
    NSString *url = [NSString stringWithFormat:@"http://mtest.ivpin.com//WPT-OpenAPI?control=LCatShop&action=upateShopCart&username=%@&shopuser=%@&productid=%@&type=0&sign=",[AppData share].username,[ShopCommonModel getShopMess].shopuser,self.productID];
    
    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
        NSDictionary *tempDic = [NSDictionary dictionary];
        tempDic = result;

        if ([[tempDic objectForKey:@"r"] isEqualToString:@"T"]) {
        NSInteger tempNum = [self.countNum integerValue];
            if (tempNum > 0) {
                tempNum --;
                self.countNum = [NSString stringWithFormat:@"%ld",tempNum];
                self.count.text = self.countNum;
                self.carLable.text = self.countNum;
                
                NSString *strNum = [NSString stringWithFormat:@"%ld",tempNum];
                // 小红点
                [[AppData share].baseTabVC theShoppingCartCount:strNum];
                    
                }

        }else{
//            UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"补货中" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
//            [aler show];
            
        }
    }];

    }// 登录
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
